<!-- 物资设备采购策划表 -->
<template>
    <div>
      <a-card>
        <title-name title="资金策划表" />
        <a-button class="mt20 mb20" type="primary" @click="addRow" v-if="queryInfo.pageType !== 'detail'"> 增行 </a-button>
        <span style="float:right">单位：（万元）</span>
        <a-table :columns="columns" :data-source="data" bordered :scroll="{ x: 'auto' }" :pagination="false">
          <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
          <!-- 期间 -->
          <span slot="period" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableDate type="month" :required="true" v-else :key="index + '-period'" :text="text" @change="onCellChange(record, 'period', $event)"></EditableDate>
          </span>
          <!-- 形象进度 -->
          <span slot="imageProgress" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableCell :required="true" v-else :key="index + '-imageProgress'" :text="text" @change="onCellChange(record, 'imageProgress', $event)" :maxLength="300"></EditableCell>
          </span>
          <!-- 本月收款 -->
          <span slot="collectionMonth" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-collectionMonth'" :text="text" @change="onCellChange(record, 'collectionMonth', $event)"></EditableNumber>
          </span>
          <!-- 累计收款 -->
          <span slot="collectionTotal" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-collectionTotal'" :text="text" @change="onCellChange(record, 'collectionTotal', $event)"></EditableNumber>
          </span>
          <!-- 本月付款 -->
          <span slot="paymentMonth" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-paymentMonth'" :text="text" @change="onCellChange(record, 'paymentMonth', $event)"></EditableNumber>
          </span>
          <!-- 累计付款 -->
          <span slot="paymentTotal" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-paymentTotal'" :text="text" @change="onCellChange(record, 'paymentTotal', $event)"></EditableNumber>
          </span>
          <!-- 甲指分包收款 -->
          <span slot="subCollection" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-subCollection'" :text="text" @change="onCellChange(record, 'subCollection', $event)"></EditableNumber>
          </span>
          <!-- 甲指分包付款 -->
          <span slot="subPayment" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-subPayment'" :text="text" @change="onCellChange(record, 'subPayment', $event)"></EditableNumber>
          </span>
          <!-- 甲指分包结余 -->
          <span slot="subSurplus" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-subSurplus'" :text="text" @change="onCellChange(record, 'subSurplus', $event)"></EditableNumber>
          </span>
          <!-- 本月经营性净现金流 -->
          <span slot="cashMonth" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-cashMonth'" :text="text" @change="onCellChange(record, 'cashMonth', $event)"></EditableNumber>
          </span>
          <!-- 累计经营性净现金流 -->
          <span slot="cashTotal" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-cashTotal'" :text="text" @change="onCellChange(record, 'cashTotal', $event)"></EditableNumber>
          </span>
          <!-- 经营性净现金流 净现金收支比(%) -->
          <span slot="cashRatio" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-cashRatio'" :text="text" @change="onCellChange(record, 'cashRatio', $event)"></EditableNumber>
          </span>
          <!-- 项目借款 借款 -->
          <span slot="projectLoan" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-projectLoan'" :text="text" @change="onCellChange(record, 'projectLoan', $event)"></EditableNumber>
          </span>
          <!-- 项目借款 还款 -->
          <span slot="projectRepayment" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-projectRepayment'" :text="text" @change="onCellChange(record, 'projectRepayment', $event)"></EditableNumber>
          </span>
          <!-- 项目借款 累计余额 -->
          <span slot="projectLoanTotal" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-projectLoanTotal'" :text="text" @change="onCellChange(record, 'projectLoanTotal', $event)"></EditableNumber>
          </span>
          <!-- 保理融资 办理 -->
          <span slot="factorageFinancingManage" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-factorageFinancingManage'" :text="text" @change="onCellChange(record, 'factorageFinancingManage', $event)"></EditableNumber>
          </span>
          <!-- 保理融资 归还 -->
          <span slot="factorageFinancingReturn" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-factorageFinancingReturn'" :text="text" @change="onCellChange(record, 'factorageFinancingReturn', $event)"></EditableNumber>
          </span>
          <!-- 保理融资 累计余额 -->
          <span slot="factorageFinancingTotal" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-factorageFinancingTotal'" :text="text" @change="onCellChange(record, 'factorageFinancingTotal', $event)"></EditableNumber>
          </span>
          <!-- 本月结余 -->
          <span slot="surplusMonth" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-surplusMonth'" :text="text" @change="onCellChange(record, 'surplusMonth', $event)"></EditableNumber>
          </span>
          <!-- 累计结余 -->
          <span slot="surplusTotal" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-surplusTotal'" :text="text" @change="onCellChange(record, 'surplusTotal', $event)"></EditableNumber>
          </span>
          <!-- 利率 -->
          <span slot="rate" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber :percentage="true"  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-rate'" :text="text" @change="onCellChange(record, 'rate', $event)"></EditableNumber>
          </span>
          <!-- 资金成本 -->
          <span slot="costFunds" slot-scope="text, record, index">
            <span v-if="queryInfo.pageType == 'detail'">{{ text }}</span>
            <EditableNumber  :min="-Infinity" :precision="2" :required="true" v-else :step="0.01" :key="index + '-costFunds'" :text="text" @change="onCellChange(record, 'costFunds', $event)"></EditableNumber>
          </span>
  
          <span slot="operation" slot-scope="text, record">
            <template>
              <a-popconfirm title="请确认是否要删除？" @confirm="() => onDelete(record)">
                <a href="javascript:;">删除</a>
              </a-popconfirm>
            </template>
          </span>
        </a-table>
        <p style="margin-top:20px">
          填表说明<br>
          1、累计结余不能小于0，如果小于0，请填列借款。发生融资的当月，融资后，累计结余应等于0。<br>
          2、所有借款，假设发生在月初，保理不计算本身已经含利息，因此不再算利息
        </p>
      </a-card>
    </div>
  </template>
  
  <script>
  import { queryPlanSubSelectDetail } from '@/api/projectManagementPlanning/projectPlanningTaskList'
  import EditableCell from '../../../projectManagementPlanning/projectPlanningTaskList/detail/components/EditableCell'
  import EditableNumber from '../../../projectManagementPlanning/projectPlanningTaskList/detail/components/EditableNumber'
  import EditableDate from '../../../projectManagementPlanning/projectPlanningTaskList/detail/components/EditableDate'
  import { getOrganizeUserListQuick } from '@/api/commons'
  
  import SelectPerson from '../../../projectManagementPlanning/projectManagementPlanningComponents/components/ProjectDesignPlanningForm/SelectPerson'
  import { planMaterialupdateByPrimaryKeySelective } from '@/api/projectManagementPlanning/projectPlanningTaskList'
  import { schemeQuerySchemeFundPlanDetail, schemeFundPlanUpdateList } from '@/api/project/plannManagement/interpenetrationPlan.js'
  
  const DEFAULT_ROW = [
    {
        'period':'',
        'imageProgress':'',
        'collectionMonth':'',
        'collectionTotal':'',
        'paymentMonth':'',
        'paymentTotal':'',
        'subCollection':'',
        'subPayment':'',
        'subSurplus':'',
        'cashMonth':'',
        'cashTotal':'',
        'cashRatio':'',
        'projectLoan':'',
        'projectRepayment':'',
        'projectLoanTotal':'',
        'factorageFinancingManage':'',
        'factorageFinancingReturn':'',
        'factorageFinancingTotal':'',
        'surplusMonth':'',
        'surplusTotal':'',
        'rate':'',
        'costFunds':'',
        guid: new Date().getTime()
    }
  ]
  
  const REQUEST_KEYS = [
    'period',
    'imageProgress',
    'collectionMonth',
    'collectionTotal',
    'paymentMonth',
    'paymentTotal',
    'subCollection',
    'subPayment',
    'subSurplus',
    'cashMonth',
    'cashTotal',
    'cashRatio',
    'projectLoan',
    'projectRepayment',
    'projectLoanTotal',
    'factorageFinancingManage',
    'factorageFinancingReturn',
    'factorageFinancingTotal',
    'surplusMonth',
    'surplusTotal',
    'rate',
    'costFunds',
  ]
  export default {
    name: 'FundPlanning',
    data() {
      return {
        queryInfo: {
          id: '',
          pageType: ''
        },
        columns: [
          {
            title: '序号',
            width: 50,
            align: 'center',
            dataIndex: 'serial',
            scopedSlots: { customRender: 'serial' }
          },
          {
            title: '期间',
            width: 150,
            align: 'center',
            dataIndex: 'period',
            scopedSlots: { customRender: 'period' }
          },
          {
            title: '形象进度',
            width: 150,
            align: 'center',
            dataIndex: 'imageProgress',
            scopedSlots: { customRender: 'imageProgress' }
          },
          {
            title: '收款',
            align: 'center',
            children: [
                {
                    title: '本月',
                    width: 100,
                    align: 'center',
                    dataIndex: 'collectionMonth',
                    scopedSlots: { customRender: 'collectionMonth' }
                },
                {
                    title: '累计',
                    width: 100,
                    align: 'center',
                    dataIndex: 'collectionTotal',
                    scopedSlots: { customRender: 'collectionTotal' }
                },
            ]
          },
          {
            title: '付款',
            align: 'center',
            children: [
                {
                    title: '本月',
                    width: 100,
                    align: 'center',
                    dataIndex: 'paymentMonth',
                    scopedSlots: { customRender: 'paymentMonth' }
                },
                {
                    title: '累计',
                    width: 100,
                    align: 'center',
                    dataIndex: 'paymentTotal',
                    scopedSlots: { customRender: 'paymentTotal' }
                },
            ]
          },
          {
            title: '甲指分包',
            align: 'center',
            children: [
                {
                    title: '收款',
                    width: 100,
                    align: 'center',
                    dataIndex: 'subCollection',
                    scopedSlots: { customRender: 'subCollection' }
                },
                {
                    title: '付款',
                    width: 100,
                    align: 'center',
                    dataIndex: 'subPayment',
                    scopedSlots: { customRender: 'subPayment' }
                },
                {
                    title: '结余',
                    width: 100,
                    align: 'center',
                    dataIndex: 'subSurplus',
                    scopedSlots: { customRender: 'subSurplus' }
                },
            ]
          },
          {
            title: '经营性净现金流',
            align: 'center',
            children: [
                {
                    title: '本月',
                    width: 100,
                    align: 'center',
                    dataIndex: 'cashMonth',
                    scopedSlots: { customRender: 'cashMonth' }
                },
                {
                    title: '累计',
                    width: 100,
                    align: 'center',
                    dataIndex: 'cashTotal',
                    scopedSlots: { customRender: 'cashTotal' }
                },
                {
                    title: '净现金收支比(%)',
                    width: 100,
                    align: 'center',
                    dataIndex: 'cashRatio',
                    scopedSlots: { customRender: 'cashRatio' }
                },
            ]
          },
          {
            title: '项目借款',
            align: 'center',
            children: [
                {
                    title: '借款',
                    width: 100,
                    align: 'center',
                    dataIndex: 'projectLoan',
                    scopedSlots: { customRender: 'projectLoan' }
                },
                {
                    title: '还款',
                    width: 100,
                    align: 'center',
                    dataIndex: 'projectRepayment',
                    scopedSlots: { customRender: 'projectRepayment' }
                },
                {
                    title: '累计余额',
                    width: 100,
                    align: 'center',
                    dataIndex: 'projectLoanTotal',
                    scopedSlots: { customRender: 'projectLoanTotal' }
                },
            ]
          },
          {
            title: '保理融资',
            align: 'center',
            children: [
                {
                    title: '办理',
                    width: 100,
                    align: 'center',
                    dataIndex: 'factorageFinancingManage',
                    scopedSlots: { customRender: 'factorageFinancingManage' }
                },
                {
                    title: '归还',
                    width: 100,
                    align: 'center',
                    dataIndex: 'factorageFinancingReturn',
                    scopedSlots: { customRender: 'factorageFinancingReturn' }
                },
                {
                    title: '累计余额',
                    width: 100,
                    align: 'center',
                    dataIndex: 'factorageFinancingTotal',
                    scopedSlots: { customRender: 'factorageFinancingTotal' }
                },
            ]
          },
          {
            title: '本月结余',
            width: 100,
            align: 'center',
            dataIndex: 'surplusMonth',
            scopedSlots: { customRender: 'surplusMonth' }
          },
          {
            title: '累计结余',
            width: 100,
            align: 'center',
            dataIndex: 'surplusTotal',
            scopedSlots: { customRender: 'surplusTotal' }
          },
          {
            title: '利率',
            align: 'center',
            dataIndex: 'rate',
            width: 100,
            scopedSlots: { customRender: 'rate' }
          },
          {
            title: '资金成本',
            width: 100,
            align: 'center',
            dataIndex: 'costFunds',
            scopedSlots: { customRender: 'costFunds' }
          },
          {
            title: '操作',
            align: 'center',
            fixed: 'right',
            width: 100,
            dataIndex: 'operation',
            scopedSlots: { customRender: 'operation' }
          }
        ],
        data: [],
        memberList: [], // 接收人
        fetching: false
      }
    },
  
    props: {
      spinning: Boolean,
      baseFormInfo: {
        type: Object,
        default: () => {}
      }
    },
  
    components: { EditableCell, EditableNumber, EditableDate, SelectPerson },
  
    computed: {},
  
    created() {
      const { id, pageType, version = '' } = this.$route.query
      /**
       * version 不存在 就是正常入口进来的。
       * 否则 就是在V1等弹窗进来的
       */
      this.queryInfo.id = id
      this.queryInfo.pageType = pageType
      this.queryInfo.version = version
      if (this.queryInfo.pageType === 'detail') {
        this.columns.pop()
      }
      this.queryPlanSubSelectDetail()
    },
  
    mounted() {},
  
    methods: {
      // 增行
      addRow() {
        const DEFAULT_ROW_ = JSON.parse(JSON.stringify(DEFAULT_ROW))
        this.data.push({ ...[...DEFAULT_ROW_][0], guid: this.getGuid() })
      },
      // 草稿
      FundPlanningSaveDrafts(formBase) {
        this.$emit('update:spinning', true)
        const query = {
          list: JSON.parse(JSON.stringify(this.data)),
          changed: this.queryInfo.pageType === 'change' ? true : false,
          schemeDetailId: this.queryInfo.id,
          status: 0,
          version: this.queryInfo.version
        }
        formBase.schemeDetailId = this.queryInfo.id
        formBase.status = 0
        this.$emit('updateBasicDetail', formBase) 
        schemeFundPlanUpdateList(query)
          .then((res) => {
            this.$emit('update:spinning', false)
            this.$message.success('保存成功')
          })
          .catch(() => {
            this.$emit('update:spinning', false)
          })
      },
      // 提交
      FundPlanningSubmit(formBase) {
        let flag = true // 是否全部填写
        this.data.map((v) => {
          REQUEST_KEYS.map((k) => {
            if (v[k] === '' || v[k] == null || v[k] == undefined || (v[k] + '').replace(/\s*/g, '') == '') {
              flag = false
            }
          })
        })
        if (this.data.length === 0) {
          this.$warning({
            title: '提示',
            content: '请至少填写一行数据',
            okText: '确定'
          })
          return
        }
        if (!flag) {
          this.$warning({
            title: '提示',
            content: '请将数据填写完整',
            okText: '确定'
          })
          return
        } else {
          this.$confirm({
            title: '提交',
            content: '请确认是否要提交？',
            onOk: async () => {
              this.$emit('update:spinning', true)
              const query = {
                list: JSON.parse(JSON.stringify(this.data)),
                changed: this.queryInfo.pageType === 'change' ? true : false,
                schemeDetailId: this.queryInfo.id,
                status: 1,
                version: this.queryInfo.version
              }
              formBase.schemeDetailId = this.queryInfo.id
              formBase.status = 1
              this.$emit('updateBasicDetail', formBase) 
              schemeFundPlanUpdateList(query)
                .then((res) => {
                  this.$emit('update:spinning', false)
                  this.$message.success('提交成功')
                  this.$emit('closePage');
                })
                .catch(() => {
                  this.$emit('update:spinning', false)
                })
            },
            onCancel: () => { this.$emit('update:spinning', false)}
          })
        }
      },
      getGuid() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
          var r = (Math.random() * 16) | 0
          var v = c === 'x' ? r : (r & 0x3) | 0x8
          return v.toString(16)
        })
      },
      // 删除item
      onDelete(item) {
        let arr = JSON.parse(JSON.stringify(this.data))
        arr = arr
          .map((v) => {
            if (v.guid !== item.guid) {
              return v
            }
          })
          .filter((v) => v && v.guid)
        this.data = arr
      },
      // 更新item
      onCellChange(item, dataIndex, value) {
        this.data.map((v) => {
          if (v.guid === item.guid) {
            v[dataIndex] = value
          }
        })
      },
      // 获取详情
      queryPlanSubSelectDetail() {
        this.$emit('update:spinning', true)
        const query = {
            schemeDetailId: this.queryInfo.id,
            latestData: true
        }
        if (this.queryInfo.version) {
            query.latestData = false
            query.version = this.queryInfo.version
        }
        schemeQuerySchemeFundPlanDetail(query)
          .then((res) => {
            if (res.data && res.data.length) {
              res.data.map((v) => (v.guid = this.getGuid()))
              this.data = res.data
            } else {
              const DEFAULT_ROW_ = JSON.parse(JSON.stringify(DEFAULT_ROW))
              this.data = [...DEFAULT_ROW_]
            }
            this.$emit('update:spinning', false)
          })
          .catch(() => {
            this.$emit('update:spinning', false)
          })
      }
    }
  }
  </script>
  <style lang='less' scoped>
  </style>